<?php

namespace app\controller;

use think\facade\Request;
use app\BaseController;
use app\ResourceModel;

class ResourceController extends BaseController
{

    protected $model;
    protected $param;
    protected $post;
    protected $pageSize;

    public function initialize()
    {
        $this->model = new ResourceModel();
        $this->param = $this->request->param();
        $this->post = $this->request->post();
        $this->pageSize = !empty($this->param['pageSize']) ? $this->param['pageSize'] : 10;
    }

    /**
     * @param Request $request
     *
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $table = $this->param['table'];
        $tableInfo = getTableHeader($table);
        $whereMust = empty($tableInfo[2]) ? [] : $tableInfo[2];
        $searchMust = [];
        if(!empty($tableInfo[3])) {
            foreach ($tableInfo[3] as $key=>$value) {
                if(!empty($this->param[$value[1]]) || $this->param[$value[1]] == '0'){
                    $searchMust[] = array($key,$value[0],$this->param[$value[1]]);
                }
            }
        }
        $orderBy = empty($tableInfo[4]) ? ['id'=>'desc'] : $tableInfo[4];

        $list = $this->model->table($table)->where($whereMust)->where($searchMust)->field($tableInfo[1])->order($orderBy)->paginate($this->pageSize, false, [])->toArray();
        $list['header'] = $tableInfo[0];
        return $this->returnPageJson(1001, '返回数据成功', $list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     *
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $this->param;
        unset($data['table']);
        $ret = $this->model->editData($data, $this->param['table']);
        return $this->returnJson($ret['code'], $ret['msg']);
    }
    /**
     * 显示指定的资源
     *
     * @param int $id
     *
     * @return \think\Response
     */
    public function read($id)
    {
        $tableInfo = getTableHeader($this->param['table']);
        $info = $this->model->table($this->param['table'])->field($tableInfo[1])->where('id', '=', $id)->find();
        if ($info) {
            return $this->returnJson(1001, '存在数据', $info);
        } else {
            return $this->returnJson(1001, '没有查询到');
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     *
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     *
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * 删除指定资源
     *
     * @param int $id
     *
     * @return \think\Response
     */
    public function delete($id)
    {
        $table = $this->param['table'];
        $result = $this->model->table($table)->whereIn('id', json_decode($this->post['ids']))->update(['del_flag' => 1]);
        $this->model->table($this->param['table'])->whereIn('id',json_decode($this->post['ids']))->useSoftDelete('delete_time',time())->delete();
        return $result ? $this->returnJson(1001, '删除成功' . $id) : $this->returnJson(1002, '删除失败' . $id);
    }

    public function test()
    {
        $aa = new ResourceModel();
    }

}
